package com.xxx.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxx.common.Result;
import com.xxx.pojo.Algorithm;
import com.xxx.pojo.LoginUser;
import com.xxx.pojo.User;
import com.xxx.service.AlgorithmService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/algorithm")
public class AlgorithmController {

    @Autowired
    private AlgorithmService algorithmService;

    /**
     * 增加算法
     * @param algorithm
     * @return
     */
    @Transactional
    @PreAuthorize("hasAnyAuthority('sys:add:algorithm')")
    @PostMapping("/save")
    public Result save(@RequestBody Algorithm algorithm){
        log.info("algorithm:{}",algorithm);

        LocalDateTime now = LocalDateTime.now();
        algorithm.setUpdateTime(now);
        LoginUser principal = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User user = principal.getUser();

        algorithm.setCreateUser(user.getUserId().longValue());

        boolean save = algorithmService.save(algorithm);
        if (!save){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.error("保存失败");
        }
        return Result.success("保存成功");

    }


    /**
     * 删除算法
     * @param id
     * @return
     */
    @Transactional
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAnyAuthority('sys:delete:algorithm')")
    public Result delById(@PathVariable Integer id){

        boolean b = algorithmService.removeById(id);
        return b? Result.success("删除成功") : Result.error("删除失败");
    }

    /**
     * 修改
     * @param algorithm
     * @return
     */
    @Transactional
    @PutMapping("/update")
    @PreAuthorize("hasAnyAuthority('sys:update:algorithm')")
    public Result update(@RequestBody Algorithm algorithm){
        log.info("Algorithm:{}",algorithm);

        boolean b = algorithmService.updateById(algorithm);
        return b? Result.success("修改成功") : Result.error("修改失败");
    }

    /**
     * 查询
     * @return
     */
    @GetMapping("/get-algorithm")
    @PreAuthorize("hasAnyAuthority('sys:select:algorithm')")
    public Result getAlgorithm(Integer pageNo, Integer pageSize,String name, Integer properties){

        IPage iPage = new Page(pageNo,pageSize);

        iPage = algorithmService.page(iPage, new LambdaQueryWrapper<Algorithm>()
                .like(name != null && name.trim() != "", Algorithm::getName, name)
                .eq(properties != null, Algorithm::getProperties, properties));


        return Result.success("查询成功", iPage);
    }

}
